package com.jade.bss.db.impl;

import com.jade.bss.DbException;
import com.jade.bss.db.JpaBaseDao;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2016-4-17
 * Time: 13:51
 * Version : 1.0
 * Remark:
 * History:
 */
@Named(value ="baseDao")
public class BaseDaoImpl implements JpaBaseDao {

    @PersistenceContext(unitName = "default")
    public EntityManager entityManager;

    public <T> void delete(Class<T> clazz, Object id)throws DbException {
        T entity = entityManager.find(clazz, id);
        entityManager.remove(entity);
    }

    public <T> void delete(Class<T> clazz, Object[] ids) throws DbException{
        T entity = null;
        for (Object id : ids) {
            entity = entityManager.find(clazz, id);
            entityManager.remove(entity);
        }
    }

    public <T> List<T> getAll(Class<T> clazz) {
        String className = clazz.getSimpleName();
        StringBuffer jpql = new StringBuffer("select o from ");
        jpql.append(className).append(" o ");
        return entityManager.createQuery(jpql.toString()).getResultList();
    }

    public <T> T getById(Class<T> clazz, Object id) {
        return entityManager.find(clazz, id);
    }

    public <T> List<T> getByIds(Class<T> clazz, Object[] ids){
        List<T> list = new ArrayList<T>();
        for(Object id : ids){
            list.add(getById(clazz, id));
        }
        return list;
    }

    public void save(Object entity) throws DbException {
        entityManager.persist(entity);
    }



    public <T> void batchSave(List<T> entitys) throws DbException{
        for(T entity : entitys){
            entityManager.persist(entity);
        }
    }

    public void update(Object entity)throws DbException {
        entityManager.merge(entity);
    }

    public int executeBySQL(String sql, Object... params) throws DbException{
        Query query = entityManager.createNativeQuery(sql);
        if (params != null) {
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    query.setParameter(i + 1, params[i]);
                }
            }
        }
        return query.executeUpdate();
    }

    public int executeJpql(String jpql, Object... objects) throws DbException{
        Query query = entityManager.createQuery(jpql);
        if (objects != null) {
            for (int i = 0; i < objects.length; i++) {
                query.setParameter(i + 1, objects[i]);
            }
        }
        return query.executeUpdate();
    }

    public <T> List<T> getByJpql(String jpql, Object... objects) {
        Query query = entityManager.createQuery(jpql);
        if (objects != null) {
            if (objects != null) {
                for (int i = 0; i < objects.length; i++) {
                    query.setParameter(i + 1, objects[i]);
                }
            }
        }
        return query.getResultList();
    }


    public <T> T getEntityByJpql(String jpql,Object... objects) {
        Query query = entityManager.createQuery(jpql);
        if (objects != null) {
            if (objects != null) {
                for (int i = 0; i < objects.length; i++) {
                    query.setParameter(i + 1, objects[i]);
                }
            }
        }

        return  SqlUtils.getSingleResult(query);
    }


    public <T> PagingList<T> queryEntity(Class<T> entityClass, Criteria criteria)
    {
        String hsql = "SELECT entity FROM "+entityClass.getCanonicalName().substring(entityClass.getCanonicalName().lastIndexOf(".")+1)+" entity";
        return SqlUtils.sqlQuery(hsql, entityManager, criteria);
    }

    public <T> PagingList<T> queryJoinEntity(String jhpl, Criteria criteria) {
        return SqlUtils.sqlQuery(jhpl, entityManager, criteria);
    }

    public <T> PagingList<T> queryEntity(Class<T> entityClass, Criteria criteria, int page, int size, Object entity) {
        return null;
    }
}
